// 课程管理store
import {
    courseGroupUpload,courseGroupGetCourses,courseGroupGetCouGro,selectCourseGroupTeacher,deselectCourseGroupTeacher,
    courseGroupDownload,courseGroupGetClasses,courseGroupDelete,
    courseGroupImport,courseGroupGetTeacher,BatchDelete,selectCourseGroupClass,deselectCourseGroupClass,
    courseGroupInfo, courseGroupModify,detailCourseIGroupInfo,courseGroupAdd
} from "../../api/courseGroup";


const state = {
    courses:[],
    classes:[],
    teachers:[],
    courseGroupInfo:[],
    courseGroupDetailInfo:{
        simpleCourseGroupInfo:{
            courseGroupId:'',
            courseGroupTime:'',
            course:'',
            teachers:[],
            classes:[]
        },
        courseGroupName:'',
        courseGroupLeader:{
            teacherId:'',
            teacherNumber:'',
            teacherName:''
        }
    },

    current: 0, // 存储批量导入的时候是第几步,
    courseGroupRecord: [], // 存储courseGroupDelete导入文件中的课程数据
    courseInfo: {
        courseGroups: [],      // 所有课程
        courseGroupTimes: [], // 开课时间
        courseGroupTeacher: [], // 课程组老师
        courseGroupLeader: [], // 课程组组长信息
        courseGroupClass: [] // 课程组班级信息
    },
    cousearchRecord: [],//存储课程组查询数据
    // inputValues:[]//动态输入表单的值
}
// mutations改变state中的数据
const mutations = {
    NULL:()=>{

    },
    //设置获取到的学院的所有课程
    SET_COURSES:(state,courses)=>{
        let data=[]
        for(let i of courses){
            data.push({
                courseId:i.courseId,
                name:i.name,
                code:i.code
            })
        }
        state.courses=data
        //console.log(data)
    },
    //设置获取到的学院的所有班级
    SET_CLASSES:(state,classes)=>{
        let data=[]
        for(let i of classes){
            data.push({
                classId:i.classId,
                className:i.className
            })
        }
        state.classes=data
    },
    //设置获取到的学院的所有老师
    SET_TEACHERS:(state,teachers)=>{
        let data=[]
        for(let i of teachers){
            data.push({
                teacherId:i.teacherId,
                teacherName:i.teacherNumber+':'+i.teacherName
            })
        }
        state.teachers=data
    },
    //设置获取到的学院的所有课程组
    SET_COURSE_GROUP_INFO: (state, info) => {
        let data=[]
        for(let i of info){
            let teachers=''
            let classes=''
            let classIds=''
            let teacherIds=''
            for(let t of i.teachers){
                teachers=teachers+t.teacherNumber+':'+t.teacherName+'\n'
                teacherIds=teacherIds+t.teacherId+','
            }
            for(let c of i.classes){
                classes=classes+c.className+'\n'
                classIds=classIds+c.classId+','
            }
            data.push({
                key:i.courseGroupId,
                course:i.course,
                courseId:i.courseId,
                courseGroupTime:i.time,
                teachers,
                classes,
                classIds,
                teacherIds
            })
        }
        state.courseGroupInfo= data
    },
    //删除某个课程组
    DELETE_COURSE_GROUP: (state, courseGroupId) => {
        let data=state.courseGroupInfo
        data=data.filter(n=>{
            return n.key!==courseGroupId
        })
        state.courseGroupInfo=data
    },
    //批量删除课程组
    MASS_DELETE_COURSE_GROUP:(state,courseGroupIds)=>{
        let data=state.courseGroupInfo
        for(let i of courseGroupIds){
            data=data.filter(n=>{
                return n.key!==i
            })
        }
        state.courseGroupInfo=data
    },
    //设置课程组详细信息
    SET_COURSE_GROUP_DETAIL_INFO:(state,data)=>{
        state.courseGroupDetailInfo.simpleCourseGroupInfo.classes=data.simpleCourseGroupInfo.classes
        state.courseGroupDetailInfo.simpleCourseGroupInfo.teachers=data.simpleCourseGroupInfo.teachers
        state.courseGroupDetailInfo.simpleCourseGroupInfo.courseGroupTime=data.simpleCourseGroupInfo.time
        state.courseGroupDetailInfo.simpleCourseGroupInfo.course=data.simpleCourseGroupInfo.course
        state.courseGroupDetailInfo.simpleCourseGroupInfo.courseId=data.simpleCourseGroupInfo.courseId
        state.courseGroupDetailInfo.simpleCourseGroupInfo.courseGroupId=data.simpleCourseGroupInfo.courseGroupId
        state.courseGroupDetailInfo.courseGroupName=data.name
        state.courseGroupDetailInfo.courseGroupLeader.teacherId=data.leader.teacherId
        state.courseGroupDetailInfo.courseGroupLeader.teacherNumber=data.leader.teacherNumber
        state.courseGroupDetailInfo.courseGroupLeader.teacherName=data.leader.teacherName
    },

    SET_CURRENT_ONE: (state) => { // 将current修改为1
        state.current = 1
    },
    SET_CURRENT_ZERO: (state) => { // 将current修改为0
        state.current = 0
    },
    SET_COURSE_GROUP_RECORD: (state, courseGroupRecord) => { // 修改导入课程中的课程数据
        // 这里把老师单独提取出来
        for (let i=0;i<courseGroupRecord.length;i++) {
            let list = []
            for(let j=0;j<courseGroupRecord[i].simpleCourseGroupInfo.teacher.length;j++){
                list.push(courseGroupRecord[i].simpleCourseGroupInfo.teacher[j].name)
            }
            courseGroupRecord[i].teacherName = list.join(',')
        }
        state.courseGroupRecord = courseGroupRecord
    },

    DELETE_COURSE_LIST_GROUP: (state, course) => { // 删除id为course的课程元素
        // console.log(12345)
        for(let i = 0; i < state.courseGroupRecord.data.length; i ++){//data不可省略
            if(state.courseGroupRecord.data[i].course === course){//遍历查询相同的数据
                state.courseGroupRecord.data.splice(i, 1)
                break;
            }
        }
    },
    MASS_DELETE_GTOUP_LIST: (state, courses) => {  //批量删除学生列表数据
        console.log(courses)
    for(let i = 0; i < state.courseGroupRecord.data.length; i++){
        // console.log(5555)
        for(let j = 0; j < courses.length; j++){
            if(state.courseGroupRecord.data[i].course === courses[j]){

                state.courseGroupRecord.data.splice(i, 1)
            }
        }
    }
},
    ADD_COURSE_GROUP:(state,newRecordInfo) =>{//新建课程组信息
        state.courseGroupRecord.data.push(newRecordInfo);
    },

    SET_COURSE_GROUP_LIST_INFO: (state, courseGroupRecord) => {
        state.courseGroupRecord = courseGroupRecord
    }
}

const actions = { // 发送请求
    courseGroupUpload({ commit }, data){ // 批量导入上传文件
        return new Promise((resolve, reject) => {

            courseGroupUpload(data).then( data => {
                if(data.code === 1){ // 上传成功
                    commit('SET_CURRENT_ONE');
                    commit('SET_COURSE_GROUP_RECORD', data.data.data);
                  //  console.log("here")
                  //  console.log(data.data.data)
                    resolve(data);
                }else{
                    reject(data)
                }
               // console.log("调用上传批量导入课程组管理接口", data)
            })
        })
    },
    courseGroupDownload(){ // 批量导入下载模板
        return new Promise((resolve, reject) => {
            courseGroupDownload().then((data) => {
                if(data.code === 1){
                    resolve(data);
                }else{
                    reject(data);
                }
               // console.log("调用下载Excel模板批量导入课程组管理接口", data)
            })
        })
    },
    backPre({commit}){ // 回到上一步，改变current的值
        commit('SET_CURRENT_ZERO')
    },
    courseGroupImport({commit, state}, token){ // 导入课程信息
        return new Promise((resolve, reject) => {
            courseGroupImport({courseGroups: state.courseGroupRecord, token: token})
                .then((data) => {
                    if(data.code === 1){
                        commit('SET_CURRENT_ZERO')
                        resolve(data);
                    }else{
                        reject(data);
                    }
                  //  console.log("导入课程信息", data)
                })
        })
    },
    courseGroupModify({commit}, data){ // 修改课程信息
        commit('EDIT_COURSE_GROUP', data);
    },

    getCourseGroupInfo({commit}, data){ // 获取批量导入编辑课程组信息
        return new Promise((resolve, reject) => {
            courseGroupInfo(data).then((data) => {
                if (data.code === 1) {
                    commit('SET_COURSE_GROUP_INFO', data.data)
                    resolve(data)
                } else {
                    reject(data)
                }
            })
        })
    },

    //新建课程组信息
    courseGroupAdd({commit}, editData){
        return new Promise((resolve, reject) => {
            courseGroupAdd(editData).then(res => {
                // 获取成功
                if (res.code === 1) {
                    commit("NULL")
                    resolve(res);
                }else{
                    reject(res.msg)
                }
            }).catch(error => {
                reject(error);
            })
        })
    },

    // 修改课程组信息
    courseGroupListModify({commit}, editData){
        return new Promise((resolve, reject) => {
            courseGroupModify(editData).then(res => {
                // 获取成功
                if (res.code === 1) {
                    commit("NULL")
                    resolve(res);
                }else{
                    reject(res.msg)
                }
            }).catch(error => {
                reject(error);
            })
        })
    },

    //获取所有课程
    courseGroupGetCourses({commit},data){
        return new Promise((resolve,reject)=>{
            courseGroupGetCourses(data).then((res)=>{
                if(res.code===1){
                    commit("SET_COURSES",res.data)
                    resolve(res)
                }else{
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //获取所有班级
    courseGroupGetClasses({commit},data){
        return new Promise((resolve,reject)=>{
            courseGroupGetClasses(data).then(res=>{
                if(res.code===1){
                    commit("SET_CLASSES",res.data)
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //获取所有老师
    courseGroupGetTeacher({commit},data){
        return new Promise((resolve,reject)=>{
            courseGroupGetTeacher(data).then(res=>{
                if(res.code===1){
                    commit("SET_TEACHERS",res.data)
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //获取所有的课程组信息
    courseGroupGetCouGro({commit},data){
        return new Promise((resolve,reject)=>{
            courseGroupGetCouGro(data).then(res=>{
                console.log(res)
                if(res.code===1){
                    commit("SET_COURSE_GROUP_INFO",res.data)
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //删除某个课程组
    courseGroupDelete({commit},data){
        return new Promise((resolve,reject)=>{
            courseGroupDelete(data).then(res=>{
                //console.log(res)
                if(res.code===1){
                    commit("NULL")
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //批量删除课程组
    BatchDelete({commit},data){
        return new Promise((resolve,reject)=>{
            BatchDelete(data).then(res=>{
                //console.log(res)
                if(res.code===1){
                    commit("NULL")
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //查看课程组的详细信息
    detailCourseIGroupInfo({commit},data){
        return new Promise((resolve,reject)=>{
            detailCourseIGroupInfo(data).then(res=>{
                //console.log(res)
                if(res.code===1){
                    commit("SET_COURSE_GROUP_DETAIL_INFO",res.data)
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //将老师添加进课程组
    selectCourseGroupTeacher({commit},data){
        return new Promise((resolve,reject)=>{
            selectCourseGroupTeacher(data).then(res=>{
                //console.log(res)
                if(res.code===1){
                    commit("NULL")
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //将老师从课程组中删除
    deselectCourseGroupTeacher({commit},data){
        return new Promise((resolve,reject)=>{
            deselectCourseGroupTeacher(data).then(res=>{
                //console.log(res)
                if(res.code===1){
                    commit("NULL")
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //将班级从课程组中删除
    deselectCourseGroupClass({commit},data){
        return new Promise((resolve,reject)=>{
            deselectCourseGroupClass(data).then(res=>{
                //console.log(res)
                if(res.code===1){
                    commit("NULL")
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //将班级添加进课程组
    selectCourseGroupClass({commit},data){
        return new Promise((resolve,reject)=>{
            selectCourseGroupClass(data).then(res=>{
                //console.log(res)
                if(res.code===1){
                    commit("NULL")
                    resolve(res)
                }else {
                    reject(res.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },
}


export default {
    namespaced: true,
    state,
    mutations,
    actions
}
